package com.xk857.simpleeffect.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.google.gson.reflect.TypeToken;
import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.domain.Result;
import com.xk857.simpleeffect.domain.vo.FriendVo;
import com.xk857.simpleeffect.utils.OkHttpUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import java.lang.reflect.Type;
import java.util.function.Consumer;

import cn.hutool.core.util.StrUtil;
import de.hdodenhof.circleimageview.CircleImageView;

public class FriendInfoActivity extends AppCompatActivity {

    private TitleBar titleBar;
    private CircleImageView headImgView;
    private LinearLayout updateNickNameLayout;
    private LinearLayout deleteLayout;
    private long customerId;
    private TextView nickNameText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.white));
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        setContentView(R.layout.activity_friend_info);
        initView();
        initData();
    }

    private void initData() {
        customerId = getIntent().getLongExtra("customerId", -1);
        String headImg = getIntent().getStringExtra("headImg");
        String nickName = getIntent().getStringExtra("nickName");
        if (customerId == -1 || StrUtil.isBlank(headImg) || StrUtil.isBlank(nickName)) {
            new MaterialDialog.Builder(this).iconRes(R.drawable.icon_error_24).title(R.string.error).content("传递信息不完整").positiveText(R.string.yes).onPositive((dialog, which) -> finish()).show();
        }
        Glide.with(this).load(headImg).into(headImgView);
        nickNameText.setText(nickName);
    }

    @Override
    protected void onResume() {
        super.onResume();
        sendInfo();
    }

    private void sendInfo() {
        String url = OkHttpUtils.BASE_URL + "/api/v1/friendship/friend/info/" + customerId;
        Type type = new TypeToken<Result<FriendVo>>() {}.getType();
        OkHttpUtils.getInstance().get(this, url, type, false, (Consumer<FriendVo>) friend -> {
            runOnUiThread(() -> {
                Glide.with(this).load(friend.getHeadImg()).into(headImgView);
                nickNameText.setText(friend.getNickName());
            });
        });
    }

    private void initView() {
        titleBar = findViewById(R.id.title_bar);
        titleBar.setLeftClickListener(v -> finish());
        headImgView = findViewById(R.id.head_img_view);
        updateNickNameLayout = findViewById(R.id.update_nick_name_layout);
        deleteLayout = findViewById(R.id.delete_layout);
        nickNameText = findViewById(R.id.nick_name_text);

        updateNickNameLayout.setOnClickListener(v -> {
            Intent intent = new Intent(this, UpdateFriendNickNameActivity.class);
            intent.putExtra("customerId", customerId);
            startActivity(intent);
        });

        deleteLayout.setOnClickListener(v -> {
            new MaterialDialog.Builder(this).iconRes(R.drawable.icon_info).title(R.string.info).content("您确定要删除该协作人吗？")
                    .positiveText(R.string.yes)
                    .negativeText(R.string.cancel)
                    .onPositive((dialog, which) -> delFriend())
                    .show();
        });
    }

    /**
     * 删除协作人
     */
    private void delFriend() {
        String url = OkHttpUtils.BASE_URL + "/api/v1/friendship/del/" + customerId;
        Type type = new TypeToken<Result<Boolean>>() {
        }.getType();
        OkHttpUtils.getInstance().get(this, url, type, true, (Consumer<Boolean>) b -> {
            runOnUiThread(() -> {
                if (b) {
                    new MaterialDialog.Builder(this).iconRes(R.drawable.icon_success).title(R.string.success).content("该协作人已删除")
                            .positiveText(R.string.yes)
                            .onPositive((dialog, which) -> finish())
                            .show();
                }
            });
        });
    }
}